package cn.edu.scnu.semart.user.controller;

import cn.edu.scnu.semart.common.result.Result;
import cn.edu.scnu.semart.common.utils.UserContext;
import cn.edu.scnu.semart.mq.constant.MqConst;
import cn.edu.scnu.semart.mq.service.RabbitService;
import cn.edu.scnu.semart.user.service.UserFollowService;
import cn.edu.scnu.semart.vo.user.FollowMessage;
import cn.edu.scnu.semart.vo.user.FollowStatsVo;
import cn.edu.scnu.semart.vo.user.FollowUserVo;
import cn.edu.scnu.semart.vo.user.SingleNumVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/follow")
public class UserFollowController {
    @Resource
    private RabbitService rabbitService;

    @Resource
    private UserFollowService userFollowService;

    // 关注
    @ApiOperation(value = "关注用户")
    @PostMapping("/confirmFollow")
    public Result<Void> follow(@Valid @RequestBody SingleNumVo body,
                               HttpServletRequest request) {
        Integer followedId = body.getId();
        Integer currentUserId = UserContext.getUserId(request);
//        boolean ok = userFollowService.follow(currentUserId, followedId);
//        return ok ? Result.ok(null) : Result.fail(null);
        rabbitService.sendMessage(MqConst.EXCHANGE_USER_FOLLOW, MqConst.ROUTING_KEY_FOLLOW,
                new FollowMessage(currentUserId, followedId));
        return Result.ok(null);
    }

    // 取关
    @ApiOperation(value = "取关用户")
    @DeleteMapping("cancle/{userId}")
    public Result<Void> unfollow(@PathVariable Integer userId,
                                 HttpServletRequest request) {
        Integer currentUserId = UserContext.getUserId(request);
//        boolean ok = userFollowService.unfollow(currentUserId, userId);
//        return ok ? Result.ok(null) : Result.fail(null);
        rabbitService.sendMessage(MqConst.EXCHANGE_USER_FOLLOW, MqConst.ROUTING_KEY_UNFOLLOW,
                new FollowMessage(currentUserId, userId));
        return Result.ok(null);
    }

    // 是否已关注
    @ApiOperation(value = "是否关注某用户")
    @GetMapping("/is/{followedId}")
    public Result<Boolean> isFollow(HttpServletRequest request,
                               @PathVariable Integer followedId) {
        Integer followerId = UserContext.getUserId(request);
        return Result.ok(userFollowService.isFollow(followerId, followedId));
    }

    // 粉丝数
    @ApiOperation(value = "粉丝数")
    @GetMapping("/fans/count/{userId}")
    public Result<Long> countFans(@PathVariable Integer userId) {
        return Result.ok(userFollowService.countFans(userId));
    }

    // 关注数
    @ApiOperation(value = "关注数")
    @GetMapping("/follows/count/{userId}")
    public Result<Long> countFollows(@PathVariable Integer userId) {
        return Result.ok(userFollowService.countFollows(userId));
    }

    @ApiOperation(value = "关注分页列表")
    @GetMapping("/followslist/{userId}")
    public Result<IPage<FollowUserVo>> pageFollows(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @PathVariable Integer userId) {
        Page<FollowUserVo> page = new Page<>(current, size);
        return Result.ok(userFollowService.pageFollows(page, userId));
    }

    @ApiOperation(value = "粉丝分页列表")
    @GetMapping("/fanslist/{userId}")
    public Result<IPage<FollowUserVo>> pageFans(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @PathVariable Integer userId) {
        Page<FollowUserVo> page = new Page<>(current, size);
        return Result.ok(userFollowService.pageFans(page, userId));
    }

    @ApiOperation(value = "同时获取用户关注数和粉丝数")
    @GetMapping("/stats/{userId}")
    public Result<FollowStatsVo> followStats(@PathVariable Integer userId) {
        FollowStatsVo vo = new FollowStatsVo();
        vo.setFollowCount(userFollowService.countFollows(userId));
        vo.setFansCount(userFollowService.countFans(userId));
        return Result.ok(vo);
    }
}